<?php

//	
// Abstract Database Access Class
//
// Version 2.0  April 4, 2007
//
// Version 2.1  April 19, 2007
//

abstract class DB {

    protected $url;
    protected $username;
    protected $password;
    protected $db;

    public function __construct() {
        if (!mysql_connect($this->url, $this->username, $this->password)) {
            throw new Exception(mysql_error());
        }
        if (!mysql_select_db($this->db)) {
            throw new Exception(mysql_error());
        }
    }

    static function query($qs) {
        /*if (get_magic_quotes_gpc()) {
    		$qs = stripslashes($qs);
    	}*/
        $result = mysql_query($qs);
        if (!$result) {
            throw new Exception('D/B Error : ' . mysql_error());
        }
        return $result;
    }

    static function fetch($result) {
        return mysql_fetch_assoc($result);
    }

    static function count($result) {
        return mysql_num_rows($result);
    }

    static function last_insert() {
        // get last inserted auto inc key
        return mysql_insert_id();
    }

    static function prt($result) {
        echo DB::ToHtml($result);
    }

    static function ToJson($result) {
        $r = array();
        while ($row=DB::fetch($result)) {
            $r[] = $row;
        }
        return json_encode($r);
    }

    static function ToXml($result) {

        // create a new XML document
        $doc = new DomDocument('1.0');

        // we want a nice output
        $doc->formatOutput = true;

        // create root node
        $root = $doc->createElement('table');
        $root = $doc->appendChild($root);

        while ($row=DB::fetch($result)) {

            // add node for each row
            $rec = $doc->createElement('row');
            $rec = $root->appendChild($rec);

            foreach($row as $colname => $colvalue) {

                // add node for each column
                $tag = $doc->createElement($colname);
                $tag = $rec->appendChild($tag);

                $value = $doc->createTextNode($colvalue);
                $value = $tag->appendChild($value);

            }
        }

        return $doc->saveXml();
    }

    static function ToHtml($result, $repeat_heading=false) {
        $first = 0;
        $html =  "<table cellpadding=2>\n";
        while ($row=DB::fetch($result)) {
            if ($first==0) {
                $html .= "<tr>\n";
                foreach($row as $key => $value) {
                    $html .= "<th valign=top align=left><font face=\"Arial, Helvetica, Geneva\" size=-1>$key</font></th>\n";
                }
                $html .= "</tr>\n";
                if ($repeat_heading) $first = 12; else $first = -1;
            }
            echo "<tr>\n";
            foreach($row as $value) {
                if (is_null($value)) $value = 'null';
                $html .= "<td valign=top><font face=\"Arial, Helvetica, Geneva\" size=-1>$value</font></td>\n";
            }
            $html .= "</tr>\n";
            $first--;
        }
        $html .= "</table>\n";

        return $html;
    }
    // REICH - 14.07.2010 - START
    static function cleanQuery ($string) {
        if (get_magic_quotes_gpc()) $string = stripslashes($string);
        $badWords = array("/delete/i", "/update/i","/union/i","/insert/i","/drop/i","/http/i","/--/i");
        $string = preg_replace($badWords, "", $string);
        if (phpversion() >= '4 .3.0') $string = mysql_real_escape_string($string);
        else $string = mysql_escape_string ($string);
        return $string;
     }

     static function getCount($result) {
        return mysql_num_rows($result);
     }
     // REICH - 14.07.2010 - END
}

?>